var id  = 0;
var pageFrom = "99";
var mainCallType = 0;
//var commitTo = 11;
//var commitTo = 18;//freeswitchCall专用
var commitTo = 8;//freeswitchCall专用,需要推数据到保利
// var createUserId = 11;
// var customerName = "李四"
var objectId = 0;
var lastMsg = "";
var sessionId = "";
var interruptCallUrl = callService+"/freeswitch/interruptCall";

//录音时间
var recordTime;


$("#callBtn").click(callPhone());

function callPhone(){
    id = Math.round(Math.random()*1000);
    objectId = Math.round(Math.random()*1000);
    var callParam = {};
    callParam["mainCallPhone"] = username;
    callParam["targetCallPhone"] = phoneNum;
    callParam["id"] = id;
    callParam["pageFrom"] = pageFrom;
    callParam["commitTo"] = commitTo;
    callParam["mainCallType"] = mainCallType;
    callParam["createUserId"] = createUserId;
    callParam["customerName"] = customerName;
    callParam["city"] = cityId;
    console.log(JSON.stringify(callParam));
    callout(callParam);
}

function callbackPC(data) {
   var messageData = JSON.parse(data);
   //当判断传递过来的数据 dataID以及是pageFrom_PC一致情况下，才做接收处理
   /*console.log("token:" + messageData["machineFrom"] + "_" + token);*/
   if (messageData["dataId"] == id) {
	   if (lastMsg != "" && (messageData["dataId"] == lastMsg.dataId && messageData["msgType"] == lastMsg.msgType)) {                  
			console.log("==========重复数据不做处理==========");
			return;                    
        }
        lastMsg = messageData;
		console.log("onMessage-> " + data.value + new Date());
		callPhoneManager(messageData);
    }
}


//用于接收通话返回的情况
function callPhoneManager(dataObj) {

    var msgType = dataObj["msgType"];    

    desc = dataObj["desc"];
    console.log("=======" + msgType + "=========");
    switch (msgType) {
        case 'COMMIT_SUCCESS':
			sessionId =  dataObj["sessionId"];
            $("#txResult").val($("#txResult").val()+"正为您发起外呼操作\n");
            /*outgoingRing();
            timeOutTask = setTimeout('connectTimeOut()', 50000);*/
            $("#callBtn").css("display","none");
            window.Android.CallIpStateDialing({"msg":"正在为您发起外呼操作"})
            break;       
        case 'MAIN_ANSWERED'://主叫应答
            break;       
        case 'TARGET_ANSWERED'://被叫应答                        
			//$("#txResult").val($("#txResult").val()+"通话中\n");
            $("#txResult").val($("#txResult").val()+"已为您接通客户\n");
            window.Android.CallIpStateAnswer({"msg":"已为您接通客户"});
            break;
        case 'MAIN_DISCONNECTED'://主叫结束通话                        
            $("#txResult").val($("#txResult").val()+"通话结束!\n");
            initBtnAndsounds();
            break;
        case 'TARGET_DISCONNECTED'://被叫结束通话
            $("#txResult").val($("#txResult").val()+"客户结束了通话!\n");
            isCallEnd = true;
            initBtnAndsounds();
            recordTime = "01:06";
            window.Android.CallIpStateHangUp({"msg":"客户结束了通话","recordTime":recordTime});
            break;
        case 'MAIN_FAILED'://主叫失败
            $("#txResult").val($("#txResult").val()+"无法呼叫您的号码...\n");
            isCallEnd = true;
            $("input[name=customerFollowCategory][value='nor']").attr("checked", true);//自动匹配“无人接通”
            initBtnAndsounds();
            window.Android.CallIpStateError({"msg":"无法呼叫您的号码"});
            break;
        case 'TARGET_FAILED'://被叫失败            
            $("#txResult").val($("#txResult").val()+"无法接通客户...\n");
            isCallEnd = true;
            initBtnAndsounds();
            window.Android.CallIpStateError({"msg":"无法接通客户..."});
            break;
        case 'COMMIT_FAILED'://提交失败            
			$("#txResult").val($("#txResult").val()+"外呼失败\n");
            initBtnAndsounds();
            window.Android.CallIpStateError({"msg":"外呼失败"});
            break;
        case 'EXCEPTION'://异常            
			$("#txResult").val($("#txResult").val()+desc+"\n");
            initBtnAndsounds();
            window.Android.CallIpStateError({"msg":desc});
            break;   
        default:
            break;
    }
}


//挂断操作
function ajaxInterrupt() {
    this.sipSession.bye();
}



 loadVersion();//加载浏览器版本是否可以进行云呼操作标识
 function loadVersion(){
    // if(!((/chrome\/(\d+)/.test(navigator.appVersion.toLowerCase())) && RegExp.$1 >= 29)) {
    //     alert("当前浏览器版本不能进行外呼操作");
    // }else{
		connWS();
	//}
}

/**
 * 停止铃声,按钮显示
 */
function initBtnAndsounds() {
    $("#callBtn").css("display","inline-block");
    outgoingStop();//停止铃声
}